﻿public class AwaitOperator
{
    public static async Task Main()
    {
        //Task<int> downloading = DownloadDocsMainPageAsync();
        //Console.WriteLine($"{nameof(Main)}: Launched downloading.");

        //int bytesLoaded = await downloading;
        //Console.WriteLine($"{nameof(Main)}: Downloaded {bytesLoaded} bytes.");

        Console.WriteLine("开始执行异步方法");

        // 调用异步方法
        var result =  DoAsyncWork();
        var resykt2=DoAsyncWork();
        Console.WriteLine(DateTime.Now);
        Console.WriteLine(DateTime.Now);
        Console.WriteLine(DateTime.Now);
        await Task.WhenAll(result,resykt2);
        Console.WriteLine($"异步方法执行完成，结果为：{await result}");
    }

    private static async Task<int> DownloadDocsMainPageAsync()
    {
        Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: About to start downloading.");

        var client = new HttpClient();
        byte[] content = await client.GetByteArrayAsync("https://docs.microsoft.com/en-us/");

        Console.WriteLine($"{nameof(DownloadDocsMainPageAsync)}: Finished downloading.");
        return content.Length;
    }
    public static async Task<int> DoAsyncWork()
    {
        Console.WriteLine("异步方法开始执行");

        // 模拟异步操作，数据库查询耗时两秒
        await Task.Delay(2000);

        Console.WriteLine("异步方法执行完成");

        return 42;
    }
}